package com.example;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.myapplication.DBHelper;
import com.example.myapplication.MyApplication;
import com.example.myapplication.R;

public class ProductDetailActivity extends AppCompatActivity {

    private TextView tvProductName, tvProductDescription, tvProductPrice;
    private Button btnBuy;
    private DBHelper dbHelper;
    private int productId;
    private int sellerId;
    private String imageUrl;
    private ImageView iamgeView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_detail);

        // 初始化组件
        tvProductName = findViewById(R.id.tvProductName);
        tvProductDescription = findViewById(R.id.tvProductDescription);
        tvProductPrice = findViewById(R.id.tvProductPrice);
        btnBuy = findViewById(R.id.btnBuy);
        iamgeView = findViewById(R.id.ivProductImage);
        dbHelper = new DBHelper(this);

        // 获取传递的数据
        Intent intent = getIntent();
        productId = intent.getIntExtra("product_id", -1);
        String productName = intent.getStringExtra("product_name");
        String productDescription = intent.getStringExtra("product_description");
        double productPrice = intent.getDoubleExtra("product_price", 0.0);
        sellerId = intent.getIntExtra("seller_id", -1);
        imageUrl = intent.getStringExtra("product_image");
        Glide.with(this)
                .load(this.getResources().getIdentifier(imageUrl, "drawable", this.getPackageName())) // 动态加载
                .placeholder(R.drawable.ic_alarm)
                .into(iamgeView);

        // 设置商品数据到页面
        tvProductName.setText(productName);
        tvProductDescription.setText(productDescription);
        tvProductPrice.setText(String.format("￥%.2f", productPrice));

        // 购买按钮点击事件
        btnBuy.setOnClickListener(v -> {
            if (productId != -1 && sellerId != -1) {
                // 向 transactions 表写入数据
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("buyer_id", MyApplication.getMyUser().getUserId());
                values.put("seller_id", sellerId);
                values.put("product_id", productId);
                values.put("status", "待支付");
                // 转换成时间日期格式
                String date = String.format("%tF %tT", System.currentTimeMillis(), System.currentTimeMillis());
                values.put("date", date); // 转换成日期格式
               // values.put("date", System.currentTimeMillis()); // 当前时间戳

                long result = db.insert("transactions", null, values);
                if (result != -1) {
                    Toast.makeText(this, "购买成功，等待支付", Toast.LENGTH_SHORT).show();
                    finish();
                } else {
                    Toast.makeText(this, "购买失败，请重试", Toast.LENGTH_SHORT).show();
                }
                db.close();
            } else {
                Toast.makeText(this, "商品信息有误", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
